
package kaizone.haining.chaobohui.widget;

import java.util.ArrayList;
import java.util.List;

import kaizone.haining.chaobohui.R;
import kaizone.haining.chaobohui.activity.DetailActivity;
import kaizone.haining.chaobohui.fragment.DetailWebFragment;
import kaizone.haining.chaobohui.json.Item;
import kaizone.haining.chaobohui.json.SubjectItem;
import kaizone.haining.chaobohui.json.Item.PicModel;
import kaizone.haining.chaobohui.util.ImageFetcher;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewTreeObserver;
import android.view.ViewGroup.LayoutParams;
import android.view.ViewTreeObserver.OnGlobalLayoutListener;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.GridView;
import android.widget.LinearLayout;

public class DetailGridView extends GridView implements
        android.widget.AdapterView.OnItemClickListener {

    public static final int BOTTOM_SPACE = 100;

    private SubjectItem mItem;

    private List<PicModel> mPicModelList;

    private MyAdapter mAdapter;

    private ImageFetcher mImageFetcher;

    public DetailGridView(Context context) {
        super(context);
        init();
    }

    public DetailGridView(Context context, AttributeSet attrs) {
        super(context, attrs);
        init();
    }

    private void init() {
        setPadding(10, 10, 10, 10);

        setOnItemClickListener(this);
        mPicModelList = new ArrayList<PicModel>();
        mAdapter = new MyAdapter();
        setAdapter(mAdapter);
        ViewTreeObserver observer = getViewTreeObserver();
        observer.addOnGlobalLayoutListener(new OnGlobalLayoutListener() {
            int setHeightFlag = 0;

            @Override
            public void onGlobalLayout() {
                // View child = getChildAt(0);
                // if (child != null && mPicModelList != null && layoutCount <
                // 1) {
                // layoutCount++;
                // child.measure(0, 0);
                // int height = child.getMeasuredHeight();
                // int count = mPicModelList.size() / 2;
                // if (count == 0) {
                // count = 1;
                // }
                // int totolHeight = height * count + BOTTOM_SPACE;
                // View parent = (View) DetailGridView.this.getParent();
                // if (parent instanceof LinearLayout) {
                // LinearLayout.LayoutParams params = new
                // LinearLayout.LayoutParams(
                // LinearLayout.LayoutParams.MATCH_PARENT, totolHeight);
                // DetailGridView.this.setLayoutParams(params);
                // }
                // }

                //
                if (mAdapter == null)
                    return;
                final int columnWidth = (DetailGridView.this.getWidth() / 2);
                final int columnHeight = columnWidth * 2 / 3;
                mAdapter.setItemHeight(columnHeight);

                if (setHeightFlag > 0)
                    return;

                if (mPicModelList == null || mPicModelList.isEmpty())
                    return;
                int count = mPicModelList.size() / 2 == 0 ? 1 : mPicModelList.size() / 2;
                int totalHeight = columnHeight * count + BOTTOM_SPACE;
                View parent = (View) DetailGridView.this.getParent();
                if (parent instanceof LinearLayout) {
                    LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(
                            LinearLayout.LayoutParams.MATCH_PARENT, totalHeight);
                    DetailGridView.this.setLayoutParams(params);
                    setHeightFlag++;
                }

            }
        });
    }

    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        if (mItem == null)
            return;
        String url = mItem.getWebUrl();
        int itemid = mItem.getId();
//        DetailActivity.openSubject(getContext(), url, itemid);
        DetailActivity.openWeb(getContext(), url);
        // Intent intent = new Intent(getContext(), DetailActivity.class);
        // Bundle bd = new Bundle();
        // bd.putInt(DetailActivity.FLAG, DetailActivity.FLAG_OPENWEB);
        // bd.putInt(Item.ID, itemid);
        // bd.putString(DetailWebFragment.WEB_FLAG, url);
        // intent.putExtras(bd);
        // getContext().startActivity(intent);
    }

    public void setData(SubjectItem item) {
        if (item == null)
            return;
        if (mPicModelList == null)
            return;
        mItem = item;
        List<PicModel> picModels = mItem.getPicModelList();
        mPicModelList.removeAll(mPicModelList);
        mPicModelList.addAll(picModels);
        mAdapter.notifyDataSetChanged();
    }

    public ImageFetcher getImageFetcher() {
        return mImageFetcher;
    }

    public void setImageFetcher(ImageFetcher mImageFetcher) {
        this.mImageFetcher = mImageFetcher;
    }

    private class MyAdapter extends BaseAdapter {

        private int mItemHeight = 0;

        private GridView.LayoutParams mImageViewLayoutParams;

        public MyAdapter() {
            mImageViewLayoutParams = new GridView.LayoutParams(LayoutParams.MATCH_PARENT,
                    LayoutParams.MATCH_PARENT);
        }

        @Override
        public int getCount() {
            return mPicModelList != null ? mPicModelList.size() : 0;
        }

        @Override
        public Object getItem(int position) {
            return mPicModelList != null ? mPicModelList.get(position) : 0;
        }

        @Override
        public long getItemId(int position) {
            return position;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            DetailGridImage gridImage = null;
            if (convertView == null) {
                gridImage = (DetailGridImage) View.inflate(getContext(), R.layout.detail_grid_img,
                        null);
                gridImage.setLayoutParams(mImageViewLayoutParams);
                convertView = gridImage;
            }
            gridImage = (DetailGridImage) convertView;

            if (gridImage.getLayoutParams().height != mItemHeight) {
                gridImage.setLayoutParams(mImageViewLayoutParams);
            }

            // test data...
            int size = mPicModelList.size();
            if (position < size) {
                PicModel picModel = mPicModelList.get(position);
                if (picModel != null) {
                    gridImage.setLevel("" + position);
                    gridImage.setDescription(mItem.getName());
                    gridImage.loadImage(picModel.sum100x000, mImageFetcher);
                }
            }
            return gridImage;
        }

        public void setItemHeight(int height) {
            if (height == mItemHeight) {
                return;
            }
            mItemHeight = height;
            mImageViewLayoutParams = new GridView.LayoutParams(LayoutParams.MATCH_PARENT,
                    mItemHeight);
            // mImageFetcher.setImageSize(height);
            notifyDataSetChanged();
        }

    }

}
